package com.micro.commonlibrary.tools;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.widget.Toast;

/*
这是他  Toast 工具类
 */
public class ToastTools {
    private ToastTools() {

    }
    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (mToast != null) {
                mToast.cancel();
            }
        }
    };


    @SuppressLint("ShowToast")
    public static void showToastLong(String text) {
        mHandler.removeCallbacks(mRunnable);
        if (mToast != null) {
            mToast.setText(text);
        } else {
            mToast = Toast.makeText(UiTools.getContext(), text, Toast.LENGTH_LONG);
        }
        mHandler.postDelayed(mRunnable, 5000);
        mToast.show();
    }

    @SuppressLint("ShowToast")
    public static void showToastLong(int text) {
        mHandler.removeCallbacks(mRunnable);
        if (mToast != null) {
            mToast.setText(text);
        } else {
            mToast = Toast.makeText(UiTools.getContext(), text, Toast.LENGTH_LONG);
        }
        mHandler.postDelayed(mRunnable, 5000);
        mToast.show();
    }

    @SuppressLint("ShowToast")
    public static void ShowToastShort(String text) {
        mHandler.removeCallbacks(mRunnable);
        if (mToast != null) {
            mToast.setText(text);
        } else {
            mToast = Toast.makeText(UiTools.getContext(), text, Toast.LENGTH_SHORT);
        }
        mHandler.postDelayed(mRunnable, 5000);
        mToast.show();
    }

    @SuppressLint("ShowToast")
    public static void ShowToastShort(int text) {
        mHandler.removeCallbacks(mRunnable);
        if (mToast != null) {
            mToast.setText(text);
        } else {
            mToast = Toast.makeText(UiTools.getContext(), text, Toast.LENGTH_SHORT);
        }
        mHandler.postDelayed(mRunnable, 5000);
        mToast.show();
    }
}
